package zhao.ben.quan.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.transaction.annotation.Transactional;
import zhao.ben.quan.dao.MemberDao;
import zhao.ben.quan.pojo.Member;
import zhao.ben.quan.service.MemberService;
import zhao.ben.quan.utils.MD5Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 会员服务实现类
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    //根据手机号查询会员信
    public Member findBytelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //添加会员信息
    public void add(Member member) {
        //用户可能从其他表单注册,携带密码
        String password = member.getPassword();
        if(password!=null){
            password = MD5Utils.md5(password);//调用方法加密
            member.setPassword(password);
        }
        memberDao.add(member);
    }


    //根据月份查询会员人数
    public List<Integer> findMemberCountByMonths(List<String> months) {

        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            String date = month+".31";
            Integer count = memberDao.findMemberCountBeforeDate(date);
            memberCount.add(count);
        }
        return memberCount;
    }
}
